package com.thti.mecool.client.command.result.plan;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.command.ICommand;
import com.thti.mecool.client.model.biz.PlanBizImpl;
import com.thti.mecool.client.protocol.plan.PlanJobProtocol;
import com.thti.mecool.client.protocol.plan.PlanJobProtocolResult;
import com.thti.mecool.client.service.Protocol;

public class MyPlanJobCommand implements ICommand {
	PlanBizImpl planBizImpl;
	List<Map<String,Object[]>> list=new ArrayList<Map<String,Object[]>> (); 
	 

	@Override
	public void doCommand(Object o) {
		PlanJobProtocolResult planProtocolResult = (PlanJobProtocolResult) o;
		if (planProtocolResult != null) {
			planBizImpl=new PlanBizImpl(Cache.getmContext());
			if (planProtocolResult.getStatus() == 1 ){
				if(planProtocolResult.getPlanJobs().size()>0){
				String proId=null;
				if(planProtocolResult.getPlanJobs().size()>0)
				planBizImpl.deleteProjectAndTask(planProtocolResult.getPlanJobs().get(0).getPlanId(),String.valueOf(BaseActivity.getCurrentUser().getId()));
				for (int i = 0; i < planProtocolResult.getPlanJobs().size(); i++) {
					if(proId==null || !proId.equals(planProtocolResult.getPlanJobs().get(i).getProjectCode())){
						list.add(saveProject(planProtocolResult.getPlanJobs().get(i)));
					}
					proId=planProtocolResult.getPlanJobs().get(i).getProjectCode();
					list.add(saveTask(planProtocolResult.getPlanJobs().get(i)));
				}
				if (null != list && list.size() > 0) {
					planBizImpl.savePlan(list);
				}
				}
				BaseActivity.setDataFlag(HandlerConstant.MyPlanJobCommand);
			}else{
				BaseActivity.setDataFlag(HandlerConstant.CommandFail);
			}
		}
	}


	public Map<String,Object[]> saveProject(PlanJobProtocol planProject) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		map.put(HandlerConstant.PROJECT_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planProject.getProjectCode(),planProject.getProjectName(),
				planProject.getPlanId(),"2",new Date()});
		return map;
	}

	public Map<String,Object[]> saveTask(PlanJobProtocol planJob) {
		Map<String,Object[]> map=new HashMap<String, Object[]>();
		//userId,projectId,taskId,taskName,startTime,endTime,storeId,storeName,planId,isType,isFinish) values(?,?,?,?,?,?,?,?,?,?,?)";	
		    map.put(HandlerConstant.TASK_SQL,new Object[]{BaseActivity.getCurrentUser().getId(),planJob.getProjectCode(),
		    		planJob.getJobId(),planJob.getJobName(),planJob.getStartTime(),
		    		planJob.getEndTime(),planJob.getStoreCode(),planJob.getStoreName(),
		    		planJob.getPlanId(),"local",0});
		return map;
	}

	
	@Override
	public String getCommandID() {
		// TODO Auto-generated method stub
		return Protocol.getAddListPlanjob;
	}

	@Override
	public Class<?> getCommandClass() {
		// TODO Auto-generated method stub
		return PlanJobProtocolResult.class;
	}

}
